package com.starsoft.quant.plugin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.starsoft.frame.base.bean.BaseAcount;
import com.starsoft.frame.base.plugin.AbstractAcountPlugin;
import com.starsoft.quant.bean.QuantAcount;
import com.starsoft.quant.service.ModelParamsService;
import com.starsoft.quant.service.QuantAcountService;
import com.starsoft.quant.service.QuantReportService;
import com.starsoft.trade.bean.TradeAcount;
import com.starsoft.trade.service.TradeAcountService;

@Component
public class QuantAcountPluginImpl extends AbstractAcountPlugin{

	@Autowired
	QuantAcountService quantAcountServie;
	
	@Autowired
	QuantReportService quantReportService;
	
	@Autowired
	ModelParamsService modelParamsService;
	@Autowired
	TradeAcountService tradeAcountService;
	
	@Override
	public String getName() {
		return "Quant";
	}
	
	@Override
	public int getOrder(){
		return 2;
	}

	public BaseAcount create(BaseAcount acount){
		return acount;
	}

	@Override
	public void update(BaseAcount acount) {
		Integer acountId = acount.getAcountId();
		TradeAcount tradeAcount = tradeAcountService.get(acountId);
		quantReportService.deleteReportBetween(acountId, null, tradeAcount.getInitDate());
	}

	@Override
	public void delete(BaseAcount acount) {
		Integer acountId = acount.getAcountId();
		QuantAcount quantAcount=quantAcountServie.getAcount(acountId);
		if(quantAcount==null){
			return;
		}
		quantReportService.deleteReportBetween(acountId, null, null);
		modelParamsService.detele(quantAcount);
		quantAcountServie.delete(quantAcount);
	}
	
	public BaseAcount copy(BaseAcount from, BaseAcount to) {
		QuantAcount quantFrom=quantAcountServie.getAcount(from.getAcountId());
		if(quantFrom==null){
			return to;
		}
		QuantAcount quantTo = quantAcountServie.getAcount(to.getAcountId());
		if(quantTo==null){
			quantTo = new QuantAcount(to.getAcountId());
		}
		quantTo.setModelCode(quantFrom.getModelCode());
		quantTo.setStartDate(quantFrom.getStartDate());
		quantTo.setEndDate(quantFrom.getEndDate());
		quantTo.setSecList(quantFrom.getSecList());
		quantTo.setRunInterval(quantFrom.getRunInterval());
		quantTo.setRunNomally(false);
		quantAcountServie.saveAcount(quantTo);
		return to;
	}

	@Override
	public BaseAcount copyAsSun(BaseAcount parent, BaseAcount sun) {
		return copy(parent, sun);
	}

}
